package de.westranger.forex.code.script;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MQLComputeIndicatorTest {

	private static final String[] files = new String[] { 
			"01_EUR_USD_Week2", "01_EUR_USD_Week3", "01_EUR_USD_Week4",
			"01_EUR_USD_Week5", "02_EUR_USD_Week1", "02_EUR_USD_Week2",
			"02_EUR_USD_Week3", "02_EUR_USD_Week4", "03_EUR_USD_Week1",
			"03_EUR_USD_Week2" };
	
	@Before
	public void setUp(){
		MQLScriptPrototype.execTestCase = false;

		MQLScriptPrototype.initMyADX = false;
		MQLScriptPrototype.initMyATR = false;
		MQLScriptPrototype.initMyMACD = false;
		MQLScriptPrototype.initMyMACDHistogram = false;
		MQLScriptPrototype.initMyMACDSignal = false;
		MQLScriptPrototype.initMyParabolicSAR = false;
		MQLScriptPrototype.initMyRSI = false;
		MQLScriptPrototype.initMyTRIX = false;
		MQLScriptPrototype.globalIterationCounter = 0;
	}
	
	@After
	public void tearDown(){
		MQLFileWriter.CloseAll();
	}

	/**
	 * kein richtigeer testcase, nur zum generien der indicatoren
	 */
	@Test
	public void testComputeIndicator(){
		for(String fname: files){
			System.out.println("\"E:\\\\EUR_USD_2013\\\\"+fname+"_indicator.csv\",");
			setUp();

			MQLScriptPrototype.outputFileIndicator = "E:\\EUR_USD_2013\\indicator\\"+fname+"_indicator.csv";
			MQLIndicator.init("E:\\EUR_USD_2013\\pre\\"+fname+"_pre.csv.gz");
			MQLScriptPrototype.initScript();

			MQLScriptPrototype.writeOutputFiles = true;
			
			MQLScriptPrototype.init();
			
			System.out.println();
			while(MQLIndicator.next()){
				MQLScriptPrototype.start();
			}
			// TODO Datei nach dem schrieben nochmal lesen um festzustellen ob alles geklappt hat mit dem schreiben
			
			MQLScriptPrototype.deinit();
			
			tearDown();	
		}
	}
	
	@Test
	public void testConfidenzInterval(){
		final double[] data = new double[]{110,112,106,90,96,118,108,114,107,90,85,84,113,105,90,104};
		
		final double[] conf = confidence95(data);
		
		assertEquals(96.54,conf[0],0.01);
		assertEquals(107.45,conf[1],0.01);
	}
	
	private double[] confidence95(final double[] data){
		double mean = 0.0;
		for(double d:data){
			mean += d;
		}
		mean /= data.length;
		
		double std = 0.0;
		for(double d:data){
			std += (d-mean)*(d-mean);
		}

		std /= data.length-1.0;
		std = Math.sqrt(std);
		std /= Math.sqrt(data.length);
		
		return new double[]{mean-(1.96*std),mean+(1.96*std)};
	}
	
	
	@Test
	public void testComputeIndicatorFullMT4(){
			setUp();
			
			MQLScriptPrototype.outputFileIndicator = ".\\data\\mql_validation_java"+"_indicator_1.csv";
			//MQLIndicator.init(".\\data\\mql_validation_java_indicator_22_short.csv");
			MQLIndicator.init("E:\\EUR_USD_2012\\mql\\mql_validation_java_indicator_21.csv.gz");
			//MQLIndicator.init("F:\\Program Files (x86)\\OANDA - MetaTrader\\tester\\files\\mql_validation_mql_raw.csv");
			MQLScriptPrototype.initScript();
			MQLScriptPrototype.writeOutputFiles = true;
			
			MQLScriptPrototype.init();
			
			System.out.println();
			while(MQLIndicator.next()){
				MQLScriptPrototype.start();
			}
			// TODO Datei nach dem schrieben nochmal lesen um festzustellen ob alles geklappt hat mit dem schreiben
			
			MQLScriptPrototype.deinit();
			
			tearDown();	
	}
}
